module Cartasur::ApplicationHelper
  # ------------ user authenitcation functions ---------------
  def admin?
    if current_user && current_user.role == "admin"
       true
    else
       false
    end
  end

  def owner?(id)
    if current_user.try(:id) == id
      true
    else
      false
    end
  end

  def admin_or_owner?(id=nil)
    if admin? || owner?(id)
      true
    else
      false
    end
  end

  def adminorpartner?
    if current_user && ( current_user.role == "admin" or current_user.role == "partner")
      true
    else
      false
    end
  end
  # ----------------- end user authentication functions -----------


  # show the objective text in a friendly format. now with localization
  def friendly_objective_text(objective_id)
    objective = Cartasur::Objective.find(objective_id)
    months = objective.months.split(/,/)
    first_month = months.try(:first)
    last_month = months.try(:last)

    days = objective.days.split(/,/)
    first_day = days.try(:first)
    last_day =  days.try(:last)

    hours = objective.time_range.split(/,/)
    first_hour = hours.try(:first)
    last_hour = hours.try(:last)

    startdate = DateTime.new(2013,first_month.try(:to_i),first_day.try(:to_i),first_hour.try(:to_i))  #year doesn't matter
    enddate = DateTime.new(2013,last_month.try(:to_i),last_day.try(:to_i),last_hour.try(:to_i))  #year doesn't matter

    friendly_text =  objective.location_name
    friendly_text << ": " << first_hour
    friendly_text << "-" << last_hour << "h" unless last_hour == first_hour

    friendly_text <<  ": " <<  (l startdate, :format => '%a')
    friendly_text << "-" << (l enddate, :format =>'%a')  unless first_day == last_day
    friendly_text << ", " << (l startdate, :format=>'%b')
    friendly_text << "-" << (l enddate, :format => '%b')   unless first_month == last_month

  end
end